package yanyan.com.tochar;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.HttpUtil;
import yanyan.com.tochar.utils.ToastUtil;

/**
 *网页源码获取
 */
public class FunsGetSourceCodeAtivity extends Activity {
   private EditText urlEd,resEd;
   private ProgressDialog loadin;
   private Spinner codingSpinner;
   private ArrayAdapter<String> codingAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_funs_getcode);
        urlEd=findViewById(R.id.code_url);
        resEd=findViewById(R.id.code_res);
        codingSpinner=findViewById(R.id.code_coding);
        List<String> codingList=new ArrayList<>();
        codingList.add("UTF-8");
        codingList.add("GBK");
        codingList.add("ISO-8859-1");
        codingList.add("GB2312");
        codingList.add("UTF-16");
        codingAdapter=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,codingList);
        codingAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        codingSpinner.setAdapter(codingAdapter);

        //统计各功能
        CoreUtil.statisticalFuns(this,"funs_get_source");

    }
    public void getSourceCode(View view){
        try{
            String url=urlEd.getText().toString();
            if(url.length()<7){
                ToastUtil.showLongToast(this,"请输入正确的网址");
                return;
            }
            url=url.replace(" ","");
            if(url.indexOf("http://")<0 && url.indexOf("https://")<0){
                url="http://"+url;
            }

            loadin=new ProgressDialog(this);
            loadin.setMessage("正在获取...");
            loadin.show();
            Call msg = HttpUtil.getMsg(url);
            msg.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            loadin.cancel();
                            resEd.setText("获取失败,请重试");
                        }
                    });
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    loadin.cancel();
                       String res=response.body().string();
                       String coding=codingSpinner.getSelectedItem().toString();
                       String finaRes=new String(res.getBytes(),coding);
                       runOnUiThread(new Runnable() {
                           @Override
                           public void run() {
                               resEd.setText(finaRes);
                           }
                       });
                }
            });

        }catch (Exception e){
            loadin.cancel();
            resEd.setText("出现错误,请检查网址是否正确以及该网页是否正常访问");
        }
    }
}
